<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class UsersController extends AppController {

    var $components = array('Session', 'Auth', 'Cookie');
    var $uses = array('User', 'UserProduct', 'Product');
	
    public function login() 
	{
		if ($this->Session->read('Auth.User')){
            $this->redirect(array('controller' => 'users', 'action' => 'input_price'));
        }
        if ($this->request->is('post')) {
            $this->request->data['User']['username'] = $this->request->data['User']['phone'] . rand(0, 50);
            $this->request->data['User']['password'] = $this->request->data['User']['phone'];
            $this->request->data['User']['is_login'] = 1;
            
            //change timezone to timezone vn
            $date = new DateTime();
            $date->add(new DateInterval('PT14H'));
            $createdDate = $date->format('Y-m-d H:i:s');
            $this->request->data['User']['created_date'] = $createdDate;

            $this->User->create();
			if ($this->User->save($this->request->data)) {
                if ($this->Auth->login()) {
                    return $this->redirect(array('controller' => 'users', 'action' => 'input_price'));
                } else {
                        $this->Session->setFlash(__('The register have a problem'));
                }
            } else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
        }
    }

    public function logout() 
	{
        $user = $this->Session->read('Auth.User');
        if($this->Auth->logout()) {
            $this->User->id = $user['user_id'];
            $this->User->saveField('is_login', 0);
            $this->redirect(array('controller' => 'users', 'action' => 'login'));
        }
    }
    
    public function input_price() 
	{
        if (!$this->Session->read('Auth.User')){
            $this->redirect('/users/login');
        }
		
		if ($this->request->is('post')) {
			$this->UserProduct->create();
			$this->request->data['UserProduct']['price_suggest'] = str_replace(',','',$this->request->data['UserProduct']['price_suggest']);
			if ($this->UserProduct->save($this->request->data)) {
				$this->Session->setFlash(__('The user product has been saved.'));
			} else {
				$this->Session->setFlash(__('The user product could not be saved. Please, try again.'));
			}
		}
		
		$product = $this->Product->find('first', array('conditions' => array('status' => 1), 'order' => 'active_time DESC'));
		$user = $this->Session->read('Auth.User');
		
		if(isset($product['Product']['product_id'])) {
			$this->UserProduct->recursive = -1;
			$userProduct = $this->UserProduct->find('first', array('conditions' => array('UserProduct.user_id' => $user['user_id'], 'UserProduct.product_id' => $product['Product']['product_id'])));

			if(isset($userProduct['UserProduct']['price_suggest'])) {
				$this->set('price_suggest', $userProduct['UserProduct']['price_suggest']);
			}
		}
		
		$this->set(compact('user', 'product'));
    }
    
}
